package com.yc.c118s3qnzf.controller;

import com.yc.c118s3qnzf.entity.House;
import com.yc.c118s3qnzf.entity.Users;
import com.yc.c118s3qnzf.mapper.HouseMapper;
import com.yc.c118s3qnzf.vo.Result;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-11-28
 */
@RestController
@RequestMapping("/c118s3qnzf/house")
public class HouseController {
    @Resource
    HouseMapper houseMapper;

    @GetMapping("query")
    @ResponseBody
    List<House>query(){
        return houseMapper.selectList(null);
    }

    @PostMapping("save")
    @ResponseBody
    public Result save(
//            @RequestBody
            @Valid House house,
            Errors error, @SessionAttribute Users logineUser){
        if (error.hasErrors()){
            return new Result(0, "字段错误", error.getAllErrors());
        }
        if (house.getId() == null) {
            house.setUserId(logineUser.getId());
            house.setTypeId(1);
            house.setStreetId(1);
            houseMapper.insert(house);
        }else {
            houseMapper.updateById(house);
        }
        return new Result(1,"房屋添加成功！",null);
    }

}
